home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14581 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: ix.netcom.com!ix-ham-oh1-12
  2. From: i82much@ix.netcom.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: 2 D arrays in C++ constructors
  5. Date: 31 Mar 1996 11:27:08 GMT
  6. Organization: Netcom
  7. Message-ID: <4jlq6c$r4q@reader2.ix.netcom.com>
  8. References: <4jlcn2$kvl@csugrad.cs.vt.edu>
  9. NNTP-Posting-Host: ix-ham-oh1-12.ix.netcom.com
  10. X-NETCOM-Date: Sun Mar 31  3:27:08 AM PST 1996
  11. X-Newsreader: News Xpress Version 1.0 Beta #4
  12.  
  13. In article <4jlcn2$kvl@csugrad.cs.vt.edu>,
  14.    rsuri@csugrad.cs.vt.edu (Raj Suri) wrote:
  15. >Ok,
  16. >I wrote a c++ class that used an array of integers.  However, I didn't know 
  17. the
  18. >size of the array until the constructor was called.  So I did this:
  19. >
  20. >class ... {
  21. >
  22. > private:
  23. >  int* ptr;
  24. >
  25. >And then in the constructor, I had this code:
  26. >
  27. >  ptr = new int[10];
  28. >
  29. >This works fine.  However, now I need to use a two dimensional array in 
  30. >which I don't know the dimensions until the constructor is called.  I can't
  31. >figure out how to declare ptr and allocate space to it such that I could do 
  32. the
  33. >following statement:
  34. >
  35. >ptr[2][3] = 4;
  36. >
  37. >Any help.. Thanks in advance.
  38. >-Raj
  39. >
  40.  
  41. I think this may work (No guarantees)
  42.  
  43. ptr = new int[a * b]
  44.  
  45. where a is your fist indece and b is your second
  46.  
  47. if ptr is a 4 by 5 array then ptr= new int [4 * 5]
  48.  
  49. *** I'm not sure if ptr = new int[a][b] will work though ***
  50.  
  51. Hope it helps my friend
  52.  
  53. i82much@ix.netcom.com
  54.